package cn.sumpu.app.coolsk.biz.impl;

import java.util.*;
//import org.slf4j.*;
import org.springframework.beans.factory.annotation.Autowired;
import cn.sumpu.app.coolsk.biz.*;
import cn.sumpu.app.coolsk.constant.Table;
import cn.sumpu.app.coolsk.dao.*;
import cn.sumpu.app.coolsk.domain.*;
import cn.sumpu.app.coolsk.exception.AppBusinessException;
import cn.sumpu.toolkit.common.util.Common;

public class ChannelBizImpl implements ChannelBiz {

//	private final static Logger logger = LoggerFactory
//			.getLogger(InfoBizImpl.class);

	@Autowired
	private IdentifierBiz identifierBiz;
	
	private ChannelDao channelDao;

	public void setChannelDao(ChannelDao channelDao) {
		this.channelDao = channelDao;
	}
	
	/**
	 * author BruceZhang
	 * @param channel	频道
	 * 添加频道
	 * @throws AppBusinessException
	 */
	@Override
	public Channel addChannel(Channel channel) throws AppBusinessException {
		//设置ID 
		if(Common.emptyStr(channel.getId())){
			channel.setId(identifierBiz.generateId(Table.CHANNEL));
		}
		//设置深度
		if("0".equals(channel.getParent())){
			channel.setDeep(1);
		}else{
			Channel c = getChannelById(channel.getParent());
			channel.setDeep(c.getDeep()+1);
		}
		channelDao.addChannel(channel);
		return channel;
	}
	/**
	 * author BruceZhang
	 * @param channel	频道
	 * 更新频道
	 * @throws AppBusinessException
	 */
	@Override
	public void updateChannel(Channel channel) throws AppBusinessException {
		channelDao.updateChannel(channel);
	}
	/**
	 * author BruceZhang
	 * @param parent
	 * @return	返回频道树结构
	 * @throws AppBusinessException
	 */
	@Override
	public List<Channel> getChannelAll(String parent)
			throws AppBusinessException {
		return channelDao.getChannelAll(parent);
	}
	//查询频道树(排除栏目)
	@Override
	public List<Channel> getOnlyChannelAll(String parent)
			throws AppBusinessException {
		return channelDao.getOnlyChannelAll(parent);
	}
	/**
	 * author BruceZhang
	 * @param parent
	 * @return	查询频道子节点
	 * @throws AppBusinessException
	 */
	@Override
	public List<Channel> getChildChannel(String parent, Integer type)
			throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("parent", parent);
		map.put("type", type);
		return channelDao.getChildChannel(map);
	}
	/**
	 * author BruceZhang
	 * @param id	频道ID
	 * @return		根据ID查询频道
	 * @throws AppBusinessException
	 */
	@Override
	public Channel getChannelById(String id) throws AppBusinessException {
		return channelDao.getChannelById(id);
	}
	//根据mark查询频道
	@Override
	public Channel getChannelByMark(String mark) throws AppBusinessException {
		return channelDao.getChannelByMark(mark);
	}

	@Override
	public List<Channel> getChildChannelList(String parent,List<Channel> list)
			throws AppBusinessException {
		List<Channel> list2=channelDao.getChannelAll(parent);
		for (Channel channel : list2) {
			if(!Common.empty(channel)){
				list.add(channel);
				getChildChannelList(channel.getId(),list);
			}
			
		}
		return list;
	}
	
}
